home *** CD-ROM | disk | FTP | other *** search
/ Explorer - Mosaic & Web / Explorer - Mosaic & Web.iso / helpers / ghostvew / src / gvwinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-03  |  13.8 KB  |  422 lines

  1. /* Copyright (C) 1993, 1994, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of GSview.
  4.   
  5.   This program is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the GSview Free Public Licence 
  9.   (the "Licence") for full details.
  10.   
  11.   Every copy of GSview must include a copy of the Licence, normally in a 
  12.   plain ASCII text file named LICENCE.  The Licence grants you the right 
  13.   to copy, modify and redistribute GSview, but only under certain conditions 
  14.   described in the Licence.  Among other things, the Licence requires that 
  15.   the copyright notice and this notice be preserved on all copies.
  16. */
  17.  
  18. /* gvwinit.c */
  19. /* Initialisation routings for Windows GSview */
  20. #include "gvwin.h"
  21.  
  22. /* Open/Save File Dialog Box */
  23. OPENFILENAME ofn;
  24. char szOFilename[MAXSTR];    /* filename for OFN */
  25. char szOFilter[256];        /* filter for OFN */
  26. /* buttons */
  27. WNDPROC lpfnButtonWndProc;    /* default button WndProc */
  28. struct buttonlist {
  29.    HWND hbutton;
  30.    struct buttonlist *next;
  31. };
  32. struct buttonlist *buttonhead, *buttontail;
  33. int real_button_width;
  34.  
  35. /* Don't start another instance - use previous instance */
  36. void
  37. gsview_init0(LPSTR lpszCmdLine)
  38. {
  39.     HWND hwnd = FindWindow(szClassName, szAppName);
  40.     BringWindowToTop(hwnd);
  41. #ifdef __WIN32__
  42.     /* skip over EXE name */
  43.     while ( *lpszCmdLine && (*lpszCmdLine!=' ')) 
  44.         lpszCmdLine++;
  45.     while ( *lpszCmdLine && (*lpszCmdLine==' ')) 
  46.         lpszCmdLine++;
  47. #endif
  48.     if (lstrlen(lpszCmdLine) != 0) {
  49.         /* open file specified on command line */
  50.         HGLOBAL hglobal;
  51.         LPSTR szFile;
  52.         hglobal = GlobalAlloc(GHND | GMEM_SHARE, lstrlen(lpszCmdLine)+1);
  53.         if (hglobal) {
  54.             szFile = GlobalLock(hglobal);
  55.         lstrcpy(szFile, lpszCmdLine);
  56.             GlobalUnlock(hglobal);
  57.         PostMessage(hwnd, WM_COMMAND, IDM_DROP, (LPARAM)hglobal);
  58.         }
  59.     }
  60. }
  61.  
  62. /* main initialisation */
  63. void
  64. gsview_init1(LPSTR lpszCmdLine)
  65. {
  66. WNDCLASS wndclass;
  67. WORD version = LOWORD(GetVersion());
  68. char *p;
  69. char workdir[MAXSTR];
  70. char filedir[MAXSTR];
  71. int length = 64;
  72.  
  73.     while (length && !SetMessageQueue(length))
  74.         length--;    /* reduce size and try again */
  75.     if (length == 0)
  76.         exit(0);    /* panic */
  77.     
  78.     if ((LOBYTE(version)<<8) + HIBYTE(version) >= 0x30a)
  79.         is_win31 = TRUE;
  80.  
  81.     /* get path to EXE */
  82.     GetModuleFileName(phInstance, szExePath, sizeof(szExePath));
  83.     if ((p = strrchr(szExePath,'\\')) != (char *)NULL)
  84.         p++;
  85.     else
  86.         p = szExePath;
  87.     *p = '\0';
  88.  
  89.     /* get path to INI file */
  90.     szIniFile[0] = '\0';
  91.     /* strcpy(szIniFile, szExePath); */
  92.     strcat(szIniFile, INIFILE);
  93.  
  94.     /* get path to help file */
  95.     strcpy(szHelpName, szExePath);
  96.     p = szHelpName + strlen(szHelpName);
  97.     LoadString(phInstance, IDS_HELPFILE, p, sizeof(szHelpName) - (p-szHelpName));
  98.  
  99.     /* help message for GetOpenFileName Dialog Box */
  100.     help_message = RegisterWindowMessage(HELPMSGSTRING);
  101.     LoadString(phInstance, IDS_TOPICROOT, szHelpTopic, sizeof(szHelpTopic));
  102.  
  103.         load_string(IDS_WAIT, szWait, sizeof(szWait));    /* generic wait message */
  104.     
  105.     /* register the window class */
  106.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  107.     wndclass.lpfnWndProc = WndImgProc;
  108.     wndclass.cbClsExtra = 0;
  109.     wndclass.cbWndExtra = sizeof(LONG);
  110.     wndclass.hInstance = phInstance;
  111.     wndclass.hIcon = LoadIcon(phInstance,MAKEINTRESOURCE(ID_GSVIEW));
  112.     wndclass.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
  113.     wndclass.hbrBackground =  GetStockObject(WHITE_BRUSH);
  114.     wndclass.lpszMenuName = NULL;
  115.     wndclass.lpszClassName = szClassName;
  116.     RegisterClass(&wndclass);
  117.  
  118.     strcpy(option.gscommand, szExePath);
  119.     strcat(option.gscommand, DEFAULT_GSCOMMAND);
  120.     strcat(option.gscommand, " -I");
  121.     strcat(option.gscommand, szExePath);
  122.     strcpy(option.gscommand+strlen(option.gscommand)-1, ";");
  123.     strcat(option.gscommand, szExePath);
  124.     strcat(option.gscommand, "fonts;");
  125.     strcat(option.gscommand, "c:\\psfonts");
  126.     option.img_origin.x = CW_USEDEFAULT;
  127.     option.img_origin.y = CW_USEDEFAULT;
  128.     option.img_size.x = CW_USEDEFAULT;
  129.     option.img_size.y = CW_USEDEFAULT;
  130.     option.unit = IDM_UNITPT;
  131.     option.quick = TRUE;
  132.     option.settings = TRUE;
  133.     option.button_show = TRUE;
  134.     option.fit_page = TRUE;
  135.     option.safer = TRUE;
  136.     option.media = IDM_LETTER;
  137.     strcpy(option.medianame, "letter");
  138.     option.user_width = 610;
  139.     option.user_height = 792;
  140.     option.epsf_clip = FALSE;
  141.     option.epsf_warn = FALSE;
  142.     option.ignore_dsc = FALSE;
  143.     option.redisplay = TRUE;
  144.     option.orientation = IDM_PORTRAIT;
  145.     option.swap_landscape = FALSE;
  146.     option.xdpi = DEFAULT_RESOLUTION;
  147.     option.ydpi = DEFAULT_RESOLUTION;
  148.     option.save_dir = TRUE;
  149.     /* defaults if entry not in gsview.ini */
  150.     hmenu = LoadMenu(phInstance, "gsview_menu");
  151.     haccel = LoadAccelerators(phInstance, "gsview_accel");
  152.     getcwd(workdir, sizeof(workdir));
  153.     /* read entries from gsview.ini */
  154.     read_profile();
  155.  
  156.     hwndimg = CreateWindow(szClassName, (LPSTR)szAppName,
  157.           WS_OVERLAPPEDWINDOW,
  158.           option.img_origin.x, option.img_origin.y, 
  159.           option.img_size.x, option.img_size.y, 
  160.           NULL, NULL, phInstance, (void FAR *)NULL);
  161.  
  162.     /* load DLL for sounds */
  163.     if (is_win31) {
  164.         /* MMSYSTEM.DLL requires Windows 3.1, so to allow gsview to run
  165.            under Windows 3.0 we can't use the import library */
  166.         hlib_mmsystem = LoadLibrary("MMSYSTEM.DLL");
  167. #ifdef __WIN32__
  168.         if (hlib_mmsystem != NULL) {
  169. #else
  170.         if (hlib_mmsystem >= HINSTANCE_ERROR) {
  171. #endif
  172.         lpfnSndPlaySound = (FPSPS)GetProcAddress(hlib_mmsystem, "sndPlaySound");
  173.         }
  174.         else {
  175.         gserror(IDS_SOUNDNOMM, NULL, MB_ICONEXCLAMATION, -1);
  176.         hlib_mmsystem = (HINSTANCE)NULL;
  177.         }
  178.     }
  179.  
  180. #ifdef __WIN32__
  181.     /* skip over EXE name */
  182.     while ( *lpszCmdLine && (*lpszCmdLine!=' ')) 
  183.         lpszCmdLine++;
  184.     while ( *lpszCmdLine && (*lpszCmdLine==' ')) 
  185.         lpszCmdLine++;
  186. #endif
  187.  
  188.     if (lstrlen(lpszCmdLine) >= 2) {
  189.         if ( ((lpszCmdLine[0] == '/') || (lpszCmdLine[0] == '-'))
  190.          &&  ((lpszCmdLine[1] == 'D') || (lpszCmdLine[1] == 'd')) ) {
  191.         debug = TRUE;
  192.         lpszCmdLine += 2;
  193.         while (*lpszCmdLine && (*lpszCmdLine == ' '))
  194.             lpszCmdLine++;
  195.         }
  196.      }
  197.     if (lstrlen(lpszCmdLine) != 0) {
  198.         /* open file specified on command line */
  199.         HGLOBAL hglobal;
  200.         LPSTR szFile;
  201.         hglobal = GlobalAlloc(GHND | GMEM_SHARE, lstrlen(lpszCmdLine)+1);
  202.         if (hglobal) {
  203.             szFile = GlobalLock(hglobal);
  204.         lstrcpy(szFile, lpszCmdLine);
  205.             GlobalUnlock(hglobal);
  206.         PostMessage(hwndimg, WM_COMMAND, IDM_DROP, (LPARAM)hglobal);
  207.         }
  208.         /* ignore last saved directory */
  209.         /* use directory of file if given, or work directory */
  210.         if ((lpszCmdLine[0] == '/') || (lpszCmdLine[0] == '-')) {
  211.         lpszCmdLine += 2;
  212.         while (*lpszCmdLine && (*lpszCmdLine == ' '))
  213.             lpszCmdLine++;
  214.         }
  215.         lstrcpy(filedir, lpszCmdLine);
  216.         if ( (p = strrchr(filedir, '\\')) == (char *)NULL ) {
  217.             if ( (p = strrchr(filedir, ':')) == (char *)NULL )
  218.             strcpy(filedir, workdir);  /* no path so use work directory */
  219.         else
  220.             *(++p) = '\0';
  221.         }
  222.         else
  223.         *(++p) = '\0';
  224.         if (!((strlen(filedir)==2) && isalpha(filedir[0]) && (filedir[1]==':')))
  225.             chdir(filedir);
  226.         if (isalpha(filedir[0]) && (filedir[1]==':'))
  227.         (void) setdisk(toupper(filedir[0])-'A');
  228.     }
  229.     play_sound(SOUND_START);
  230.         if (changed_version) {
  231.         message_box("The installed version of GSview has changed.  \
  232. Please read the Installation help and then correctly set\r\
  233. Options | Ghostscript Command", 0);
  234.         load_string(IDS_TOPICINSTALL, szHelpTopic, sizeof(szHelpTopic));
  235.         get_help();
  236.         }
  237. }
  238.  
  239. /* create gsview window menu bar and buttons */
  240. void
  241. gsview_create()
  242. {
  243. int i;
  244. char cReplace;
  245. WNDCLASS wndclass;
  246. HGLOBAL hglobal;
  247. short FAR *pButtonID;
  248. TEXTMETRIC tm;
  249. HDC hdc;
  250. HWND hbutton;
  251. WNDPROC    lpfnMenuButtonProc;
  252. POINT char_size;        /* size of default text characters */
  253. POINT button_size, button_shift;
  254. char thismedia[20];
  255.  
  256.     /* setup OPENFILENAME struct */
  257.     if (!LoadString(phInstance, IDS_FILTER, szOFilter, sizeof(szOFilter)-1))
  258.         return;
  259.     cReplace = szOFilter[strlen(szOFilter)-1];
  260.     for (i=0; szOFilter[i] != '\0'; i++)
  261.         if (szOFilter[i] == cReplace)
  262.         szOFilter[i] = '\0';
  263.     ofn.lStructSize = sizeof(OPENFILENAME);
  264.     ofn.hwndOwner = hwndimg;
  265.     ofn.lpstrFilter = szOFilter;
  266.     ofn.nFilterIndex = FILTER_PS;
  267.     ofn.lpstrFile = szOFilename;
  268.     ofn.nMaxFile = sizeof(szOFilename);
  269.     ofn.lpstrFileTitle = (LPSTR)NULL;
  270.     ofn.nMaxFileTitle = 0;
  271.     ofn.lpstrTitle = (LPSTR)NULL;
  272.     ofn.lpstrInitialDir = (LPSTR)NULL;
  273.     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_SHOWHELP;
  274.     LoadString(phInstance, IDS_TOPICROOT, szHelpTopic, sizeof(szHelpTopic));
  275.  
  276.     /* add menu to image window */
  277.     SetMenu(hwndimg, hmenu);
  278.  
  279.     /* get default text size */
  280.     hdc = GetDC(hwndimg);
  281.     GetTextMetrics(hdc,(LPTEXTMETRIC)&tm);
  282.     display.planes = GetDeviceCaps(hdc, PLANES);
  283.     display.bitcount = GetDeviceCaps(hdc, BITSPIXEL);
  284.     
  285.     ReleaseDC(hwndimg,hdc);
  286.     char_size.x = tm.tmAveCharWidth;
  287.     char_size.y = tm.tmHeight;
  288.  
  289.     /* set size of info area, buttons and offset to child window */
  290.     info_rect.left = 0;
  291.     info_rect.right = info_rect.left + 60 * char_size.x;
  292.     info_rect.top = 0;
  293.     info_rect.bottom = char_size.y;
  294.     button_size.x = 24;
  295.     button_size.y = 24;
  296.     button_shift.x = 0;
  297.     button_shift.y = button_size.y - 1;
  298.     button_rect.top = info_rect.bottom;
  299.     button_rect.left = -1;
  300.     button_rect.right = button_size.x - 2;
  301.     button_rect.bottom = 0;        /* don't care */
  302.     real_button_width = button_rect.right;
  303.     if (!option.button_show)
  304.         button_rect.right = 0;
  305.     img_offset.x = button_rect.right + (option.button_show ? 1 : 0);
  306.     img_offset.y = info_rect.bottom + 1;
  307.     info_file.x = info_rect.left + 2;
  308.     info_file.y = 0;
  309.     info_coord.left = info_rect.left + 20 * char_size.x;
  310.     info_coord.right = info_rect.left + 34 * char_size.x;
  311.     info_coord.top = 0;
  312.     info_coord.bottom = char_size.y;
  313.     info_page.x = info_rect.left + 36 * char_size.x + 2;
  314.     info_page.y = 0;
  315.  
  316.     /* check menu items */
  317.     for (i=IDM_LETTER; i<IDM_USERSIZE; i++) {
  318.         get_menu_string(IDM_MEDIAMENU, i, thismedia, sizeof(thismedia));
  319.         if (!stricmp(thismedia, option.medianame)) {
  320.         break;
  321.         }
  322.     }
  323.     option.media = i;
  324.     strncpy(option.medianame,thismedia,sizeof(option.medianame));
  325.     CheckMenuItem(hmenu, option.unit, MF_BYCOMMAND | MF_CHECKED);
  326.     CheckMenuItem(hmenu, option.media, MF_BYCOMMAND | MF_CHECKED);
  327.     CheckMenuItem(hmenu, option.orientation, MF_BYCOMMAND | MF_CHECKED);
  328.     CheckMenuItem(hmenu, gsview_depth_to_menu(option.depth), MF_BYCOMMAND | MF_CHECKED);
  329.     if (option.epsf_clip)
  330.         CheckMenuItem(hmenu, IDM_EPSFCLIP, MF_BYCOMMAND | MF_CHECKED);
  331.     if (option.epsf_warn)
  332.         CheckMenuItem(hmenu, IDM_EPSFWARN, MF_BYCOMMAND | MF_CHECKED);
  333.     if (option.ignore_dsc)
  334.         CheckMenuItem(hmenu, IDM_IGNOREDSC, MF_BYCOMMAND | MF_CHECKED);
  335.     if (option.swap_landscape)
  336.         CheckMenuItem(hmenu, IDM_SWAPLANDSCAPE, MF_BYCOMMAND | MF_CHECKED);
  337.     if (option.save_dir) 
  338.         CheckMenuItem(hmenu, IDM_SAVEDIR, MF_BYCOMMAND | MF_CHECKED);
  339.     if (option.button_show) 
  340.         CheckMenuItem(hmenu, IDM_BUTTONSHOW, MF_BYCOMMAND | MF_CHECKED);
  341.     if (option.fit_page) 
  342.         CheckMenuItem(hmenu, IDM_FITPAGE, MF_BYCOMMAND | MF_CHECKED);
  343.     if (option.quick) 
  344.         CheckMenuItem(hmenu, IDM_QUICK, MF_BYCOMMAND | MF_CHECKED);
  345.     if (option.safer) 
  346.         CheckMenuItem(hmenu, IDM_SAFER, MF_BYCOMMAND | MF_CHECKED);
  347.     if (option.redisplay) 
  348.         CheckMenuItem(hmenu, IDM_AUTOREDISPLAY, MF_BYCOMMAND | MF_CHECKED);
  349.     if (option.settings)
  350.         CheckMenuItem(hmenu, IDM_SAVESETTINGS, MF_BYCOMMAND | MF_CHECKED);
  351.  
  352.     hcWait = LoadCursor((HINSTANCE)NULL, IDC_WAIT);
  353.  
  354.     /* add buttons */
  355.     lpfnMenuButtonProc = (WNDPROC)MakeProcInstance((FARPROC)MenuButtonProc, phInstance);
  356.     GetClassInfo((HINSTANCE)NULL, "button", &wndclass);    /* get default button class info */
  357.     lpfnButtonWndProc = wndclass.lpfnWndProc;
  358.     
  359.     hglobal = LoadResource(phInstance, FindResource(phInstance, MAKEINTRESOURCE(IDR_BUTTON), RT_RCDATA));
  360.     if ( (pButtonID = (short FAR *)LockResource(hglobal)) == (short FAR *)NULL)
  361.         return;
  362.     
  363.     for (i=0; pButtonID[i]; i++) {
  364.         hbutton = CreateWindow("button", NULL,
  365.             WS_CHILD | (option.button_show ? WS_VISIBLE : 0) | BS_OWNERDRAW,
  366.             button_rect.left + i * button_shift.x,
  367.             button_rect.top  + i * button_shift.y,
  368.             button_size.x, button_size.y,
  369.             hwndimg, (HMENU)pButtonID[i],
  370.             phInstance, NULL);
  371.         SetWindowLong(hbutton, GWL_WNDPROC, (LONG)lpfnMenuButtonProc);
  372.         if (hbutton) {
  373.         if (buttonhead == (struct buttonlist *)NULL)
  374.             buttontail = buttonhead = (struct buttonlist *)malloc(sizeof(struct buttonlist));
  375.         else {
  376.             buttontail->next = (struct buttonlist *)malloc(sizeof(struct buttonlist)); 
  377.             buttontail = buttontail->next;
  378.         }
  379.         buttontail->hbutton = hbutton;
  380.         buttontail->next = NULL;
  381.         }
  382.     }
  383.     FreeResource(hglobal);
  384. }
  385.  
  386. void
  387. show_buttons(void)
  388. {
  389. struct buttonlist *bp = buttonhead;
  390. RECT rect;
  391.     button_rect.right = option.button_show ? real_button_width : 0;
  392.     img_offset.x = button_rect.right + (option.button_show ? 1 : 0);
  393.     if (!option.button_show) {
  394.         while (bp) {
  395.             ShowWindow(bp->hbutton, SW_HIDE);
  396.             bp = bp->next;
  397.         }
  398.         if (hwndimgchild == (HWND)NULL) {
  399.             GetClientRect(hwndimg, &rect);
  400.             rect.right = real_button_width + 1;
  401.             rect.top = button_rect.top;
  402.             InvalidateRect(hwndimg, &rect, TRUE);
  403.             UpdateWindow(hwndimg);
  404.         }
  405.     }
  406.     GetClientRect(hwndimg, &rect);
  407.     SetWindowPos(hwndimgchild, (HWND)NULL, rect.left+img_offset.x, rect.top+img_offset.y,
  408.         rect.right-img_offset.x, rect.bottom-img_offset.y, 
  409.         SWP_NOZORDER | SWP_NOACTIVATE);
  410.     rect.right = real_button_width + 1;
  411.     rect.top = button_rect.top;
  412.     if (option.button_show) {
  413.         InvalidateRect(hwndimg, &rect, FALSE);
  414.         UpdateWindow(hwndimg);
  415.         while (bp) {
  416.             ShowWindow(bp->hbutton, SW_SHOWNA);
  417.             bp = bp->next;
  418.         }
  419.     }
  420. }
  421.  
  422.